<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>封装节流函数(页面滚动事件)</title>
    <style>
      body {
        height: 10000px;
      }
    </style>
  </head>
  <body>
    <div></div>
    <script>
      function throttle(callback, wait = 500) {
        let lastTime = 0
        return function (...arg) {
          let nowTime = Date.now()
          if (nowTime - lastTime > wait) {
            callback.apply(this, arguments)
            lastTime = nowTime
          }
        }
      }
      document.addEventListener(
        'scroll',
        throttle(function (event) {
          console.log('页面滚动了', this, event)
        })
      )
    </script>
  </body>
</html>
